package com.etime.shycourse.controller;

import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.config.Tools;
import com.etime.shycourse.dto.CurrentUser;
import com.etime.shycourse.pojo.StudyRecord;
import com.etime.shycourse.services.interfaces.StudyRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 学习记录接口
 */
@Api(tags = "学习记录接口")
@RestController
@RequestMapping("/study/record")
public class StudyRecordController {

    @Autowired
    private StudyRecordService studyRecordService;

    @Autowired
    private Tools tools;

    @ApiOperation("保存学习记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name="ruserid",value = "用户id"),
            @ApiImplicitParam(name="rcourseid",value = "课程id"),
            @ApiImplicitParam(name="rlessonid",value = "课时id"),
            @ApiImplicitParam(name="rlessonrecord",value = "已经学习的时长，单位（秒）"),
            @ApiImplicitParam(name="rlessonstate",value = "学习状态，0-未学习；1-学习中，2-已完成")
    })
    @PostMapping("/save")
    public BaseMessage saveStudyRecode(@RequestBody StudyRecord record, HttpServletRequest request){
        BaseMessage msg = new BaseMessage();
        if(record.getRlessonid()==null){
            msg.setCode(0);
            msg.setMsg("不需要保存学习记录");
            return msg;
        }
        String token = request.getHeader ("token");
        if(token==null){
            msg.setCode(-2);
            msg.setMsg("授权登录过期");
        }else{
            CurrentUser user = tools.getCurrentUserFromRedis(token);
            record.setRuserid(user.getUser().getUserid());
            int rtn = studyRecordService.saveStudyRecord(record);
            msg.setCode(rtn);
            msg.setMsg("保存学习记录成功");
        }
        return msg;
    }

    /**
     * 查询用户某个课时的学习记录
     * @param lessonid 课时id
     * @param request
     * @return
     */
    @ApiOperation("查询用户指定课时的学习记录")
    @ApiImplicitParam(name="lessonid",value = "课时id")
    @GetMapping("/getbylessonid")
    public PojoMessage<StudyRecord> selectStudyRecordByLessonid(Integer lessonid,HttpServletRequest request){
        PojoMessage<StudyRecord> msg = new PojoMessage<>();
        String token = request.getHeader ("token");
        if(token==null){
            msg.setCode(-2);
            msg.setMsg("授权登录过期");
        }else{
            CurrentUser user = tools.getCurrentUserFromRedis(token);
            StudyRecord record = studyRecordService.selectStudyRecordByLessonId(user.getUser().getUserid(),lessonid);
            msg.setCode(1);
            msg.setData(record);
        }
        return msg;
    }

    /**
     * 查询用户指定课程的学习记录（最后学习记录）
     * @param courseid 课程id
     * @param request
     * @return
     */
    @ApiOperation("查询用户指定课程的学习记录（最后学习记录）")
    @ApiImplicitParam(name="courseid",value = "课程id")
    @GetMapping("/getbycourseid")
    public PojoMessage<StudyRecord> selectStudyRecordByCourseId(Integer courseid,HttpServletRequest request){
        PojoMessage<StudyRecord> msg = new PojoMessage<>();
        String token = request.getHeader ("token");
        if(token==null){
            msg.setCode(-2);
            msg.setMsg("授权登录过期");
        }else{
            CurrentUser user = tools.getCurrentUserFromRedis(token);
            StudyRecord record = studyRecordService.selectStudyRecordByCourseId(user.getUser().getUserid(),courseid);
            msg.setCode(1);
            msg.setData(record);
        }
        return msg;
    }
}
